package cn.wqk.rrtf.controller;

import cn.wqk.rrtf.mapper.ChatMapper;
import cn.wqk.rrtf.mapper.MemberMapper;
import cn.wqk.rrtf.mapper.UserMapper;
import cn.wqk.rrtf.pojo.Chat;
import cn.wqk.rrtf.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class ChatController {
    @Autowired
    private ChatMapper chatMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/toChat")
    public String toChat(Model model){
        System.out.println("asdsa");
        List<Chat> chatList = chatMapper.selectAllChat();
        model.addAttribute("chatList",chatList);
        return "user/chat";
    }

    @RequestMapping("/TypeChat")
    public String TypeChat(String type,Model model){
        System.out.println(type);
        List<Chat> chatList = chatMapper.selectAllChatByType(type);
        model.addAttribute("chatList",chatList);
        return "user/chat";
    }


    @RequestMapping("/JoinChat")
    public String JoinChat(Integer chatId, HttpServletResponse response,Model model
//                           HttpSession session,
    ) throws IOException {
//        User user = (User)session.getAttribute("user");
        Member member = new Member();
//        member.setUserId(user.getUserId());
        member.setUserId(1);
        List<Member> memberList =  memberMapper.selectMemberByCondition(member);
        for (Member member1 : memberList) {
            int i = chatMapper.insertChatByMemberId(chatId, member1.getMemberId());
            System.out.println(i);
        }


        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("<script type='text/javascript'>alert('加入成功!');</script>");
        List<Chat> chatList = chatMapper.selectAllChat();
        model.addAttribute("chatList",chatList);
        return "user/chat";
    }

    @RequestMapping("/ExitChat")
    public String ExitChat(Integer chatId, HttpServletResponse response,Model model
//                           HttpSession session,
                         ) throws IOException {
//        User user = (User)session.getAttribute("user");
        Member member = new Member();
//        member.setUserId(user.getUserId());
        member.setUserId(1);
        List<Member> memberList =  memberMapper.selectMemberByCondition(member);
        for (Member member1 : memberList) {
            int i = chatMapper.deleteChatByMemberId(chatId,member1.getMemberId());
            System.out.println(i);
        }
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("<script type='text/javascript'>alert('退出成功!');</script>");
        List<Chat> chatList = chatMapper.selectAllChat();
        model.addAttribute("chatList",chatList);

        return "user/chat";
    }

    @RequestMapping("/ChatDetail")
    public String ChatDetail(){
        return "user/chat_details";
    }
}
